﻿using Microsoft.CodeAnalysis.Operations;
using NPOI.Util;
using SmartPlant.Model;
using SmartPlant.Model.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web;
using static System.Formats.Asn1.AsnWriter;

namespace SmartPlant.Services
{
    public class LoopService : BaseService, ILoopService
    {

        public async Task<Loop> GetLoopAsync(decimal proId, string loopId)
        {
            string uri = $"/api/Project/{proId}/loop-api/{loopId}";
            var res = await GetAsync<Loop>(uri);
            return res.data;
        }

 

        public async Task<RESTfulResult<decimal?>> CreateLoopAsync(decimal proId, decimal unitId, string loopName, decimal copyLoopId = 0)
        {
            string uri = $"/api/Project/{proId}/loop-api/{unitId}/{Uri.EscapeDataString(loopName)}/{copyLoopId}";
            var r = await PostAsync<decimal?>(uri);
            return r;
        }

        public async Task<bool> UpdateLoopAsync(decimal proId, Loop loopItem)
        {
            string uri = $"/api/Project/{proId}/loop-api";
            var r = await PutBodyAsync<bool, Loop>(uri, loopItem);
            return r.data;
        }

        public async Task<bool> DeleteLoopAsync(decimal proId, string objid)
        {
            string uri = $"/api/Project/{proId}/loop-api/{objid}";
            var r = await DeleteAsync<bool>(uri);
            return r.data;
        }


        /// <summary>
        /// 回路下的仪表
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="unitId"></param>
        /// <param name="select"></param>
        /// <param name="orderby"></param>
        /// <returns></returns>
        public async Task<List<Loop>> GetUnitLoopsAsync(decimal proId, string unitId, string select = "loop_id,loop_name", string orderby = "loop_name")
        {
            string sqlwhere = $"unit_id='{unitId}' and ENG_REF_ID='0'";
            string uri = $"/api/Project/{proId}/loop-api?sqlwhere={sqlwhere}&select={select}&orderby={orderby}";
            var res = await GetAsync<List<Loop>>(uri);
            return res.data;
        }
    }
}